package com.company.eos.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.eos.mapper.AuditFlowMapper;
import com.company.eos.mapper.UserMapper;
import com.company.eos.domain.*;
import com.company.eos.service.AuditFlowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Real
 * @description 针对表【tb_audit_flow(OA审批流主表)】的数据库操作Service实现
 * @createDate 2022-04-28 13:00:00
 */
@Service
public class AuditFlowServiceImpl extends ServiceImpl<AuditFlowMapper, AuditFlow>
        implements AuditFlowService {

    @Resource
    private AuditFlowMapper auditFlowMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public int insertQuitAudit(AuditQuit auditQuit) {
        AuditFlow flow = new AuditFlow();
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getId, auditQuit.getApplyUserId()));
        flow.setApplyTitle(user.getName() + "的离职申请");
        flow.setApplyStatus("待审批");
        flow.setApplyTime(new Date());
        flow.setApplyType("员工离职");
        flow.setApplyUserId(auditQuit.getApplyUserId());
        flow.setApplyReason(auditQuit.getApplyReason());
        auditFlowMapper.insert(flow);
        return flow.getId();
    }

    @Override
    public int insertLeaveAudit(AuditLeave auditLeave) {
        AuditFlow flow = new AuditFlow();
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getId, auditLeave.getUserId()));
        flow.setApplyTitle(user.getName() + "的请假申请");
        flow.setApplyStatus("待审批");
        flow.setApplyTime(new Date());
        flow.setApplyType("员工请假");
        flow.setApplyUserId(auditLeave.getUserId());
        flow.setApplyReason(auditLeave.getReason());
        auditFlowMapper.insert(flow);
        return flow.getId();
    }

    @Override
    public int insertExpenseAudit(AuditExpense expense) {
        AuditFlow flow = new AuditFlow();
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getId, expense.getUserId()));
        flow.setApplyTitle(user.getName() + "的报销申请");
        flow.setApplyStatus("待审批");
        flow.setApplyTime(new Date());
        flow.setApplyType("报销申请");
        flow.setApplyUserId(expense.getUserId());
        auditFlowMapper.insert(flow);
        return flow.getId();
    }

    @Override
    public int insertMeetingAudit(AuditMeeting meeting) {
        AuditFlow flow = new AuditFlow();
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getId, meeting.getCreatorId()));
        flow.setApplyTitle(user.getName() + "的会议申请");
        flow.setApplyStatus("待审批");
        flow.setApplyTime(new Date());
        flow.setApplyType("会议申请");
        flow.setApplyUserId(meeting.getCreatorId());
        flow.setApplyReason(meeting.getDescription());
        auditFlowMapper.insert(flow);
        return flow.getId();
    }
}




